package com.lingkang.flychat.ui.activity.login;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lingkang.flychat.R;
import com.lingkang.flychat.dto.ResponseResult;
import com.lingkang.flychat.entity.User;
import com.lingkang.flychat.http.HttpUtils;
import com.lingkang.flychat.http.ResponseListenter;
import com.lingkang.flychat.http.service.UserService;
import com.lingkang.flychat.ui.activity.base.BaseActivity;
import com.lingkang.flychat.ui.activity.main.MainActivity;
import com.orhanobut.hawk.Hawk;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        ButterKnife.bind(this);
    }


    @OnClick(R.id.btn_login)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                login();
                break;
            default:
                break;
        }
    }

    private void login() {
        if (isEmpty(etUsername)) {
            ToastUtils.showShort(R.string.login_war_username);
            return;
        }
        if (isEmpty(etPassword)) {
            ToastUtils.showShort(R.string.login_war_password);
            return;
        }
        //登录业务
        HttpUtils.request(HttpUtils.createApi(UserService.class).login(getStr(etUsername), getStr(etPassword)), new ResponseListenter<ResponseResult>() {
            @Override
            public void onSuccess(ResponseResult data) {
                if (data.getCode() == 0) {
                    //保存 token
                    Hawk.put("token", data.getData().toString());
                    Hawk.put("loginTime", System.currentTimeMillis());
                    getInfo();
                } else {
                    ToastUtils.showShort(data.getMessage());
                }
            }

            @Override
            public void onFail(String data) {
                ToastUtils.showShort(data);
            }
        });

    }

    private void getInfo() {
        HttpUtils.request(HttpUtils.createApi(UserService.class).info(), new ResponseListenter<ResponseResult<User>>() {
            @Override
            public void onSuccess(ResponseResult<User> data) {
                if (data.getCode() == 0) {
                    Hawk.put("info", data.getData());
                    ToastUtils.showShort(R.string.login_success);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                    startIntent(MainActivity.class, true);
                } else {
                    ToastUtils.showShort(data.getMessage());
                }
            }

            @Override
            public void onFail(String data) {
                ToastUtils.showShort(data);
            }
        });
    }


    private boolean isEmpty(EditText editText) {
        if (editText == null || StringUtils.isEmpty(editText.getText().toString()))
            return true;
        else
            return false;
    }

    private String getStr(EditText editText) {
        return editText.getText().toString();
    }

}
